package com.project.common.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;
import com.project.common.MemoryConstants;
import com.project.common.util.StringUtil;
import com.project.service.front.preSchool.INewsTitleTagService;
import com.project.service.impl.front.preSchool.NewsTitleTagServiceImpl;
import com.project.webapp.form.BaseForm;
import com.project.webapp.form.front.preSchool.ArticleForm;

/**
 * 公共 CourseTitle Tag实作
 * @author Sn
 *
 */
public class NewsCourseTitle extends Component 
{
	public Log log = LogFactory.getLog(this.getClass());
	
	private HttpServletRequest request;
	// 显示第几层
	private String count = null;
	// 一共显示几笔数据
	private String recordCount = null;
	// 类别，course:课程
	private String type = null;

	public NewsCourseTitle(ValueStack vs, HttpServletRequest request) 
	{
		super(vs);
		this.request = request;
	}

	@Override
	public boolean start(Writer writer) 
	{
		boolean result = super.start(writer);
		try 
		{
			StringBuffer str = new StringBuffer();
			INewsTitleTagService newsTitleTagService = new NewsTitleTagServiceImpl();
			ArticleForm artForm = new ArticleForm();
			artForm.setCount(count);// 第几层
			artForm.setRecordCount(recordCount);// 显示第几条数据
			artForm.setType(type);// 类别，课程
			// 接受课程的信息
			List<BaseForm> list = newsTitleTagService.query(artForm, log,
					MemoryConstants.PRE_SCHOOL_HOME_COURSE_NEWS + count, 0,
					request.getSession().getServletContext());

			int i = 0;

			int temp = Integer.parseInt(artForm.getRecordCount());
			ArticleForm art = null;
			for (BaseForm bf : list) 
			{
				art = (ArticleForm) bf;// 强制转换
				
				try 
				{					
					art.setArea(this.type(art.getArea()));
					
				} catch (Exception e) 
				{
					art.setArea("洪山卓刀泉");
				}
				if(art.getArea()==null)
				{
					art.setArea(" 洪山卓刀泉");
				}
								
				String title = StringUtil.substringUTF8(art.getArticle_title(), 34);
				String pic=art.getPic_link()==null?"&nbsp;&nbsp;":"<img src='\\front/preSchool/style/image/tb2.png'/>";
				//取出第一条数据
				if (i == 0) 
				{
					i++;
					str.append("<li class='mt4'><a target='blank' href='front/readnew.action?id="+art.getArticle_id()+"' class='red'>【")
							.append(art.getMenu_name())
							.append("】</a><a target='_blank'  href='front/readnew.action?id="+art.getArticle_id()+"'>"
									+ title
									+ "</a>&nbsp;<a target='_blank' href='front/readnew.action?id="+art.getArticle_id()+"'>"+pic+"")
							.append("</a>&nbsp;<a href='#' class='course_color'>"
									+ art.getArea()
									+  "</a></li>");
					//第四条新闻要进行特别处理
				} else if (i == 4) 
				{
					i++;
					str.append("<li class='mt10'><a target='_blank' href='front/readnew.action?id="+art.getArticle_id()+"' class='red'>【")
							.append(art.getMenu_name())
							.append("】</a><a target='_blank' href='front/readnew.action?id="+art.getArticle_id()+"'>"
									+ title
									+ "</a>&nbsp;<a target='_blank' href='front/readnew.action?id="+art.getArticle_id()+"'>"+pic+"")
									.append("</a>&nbsp;<a target='_blank' href='#' class='course_color'>"
									+ art.getArea()
									+ " &nbsp;"
									 + "</a></li>");
				} else 
				{
					i++;
					str.append("<li><a target='_blank' href='readnew.action?id="+art.getArticle_id()+"' class='red'>【")
							.append(art.getMenu_name())
							.append("】</a><a target='_blank' href='front/readnew.action?id="+art.getArticle_id()+"'>"
									+ title
									+ "</a>&nbsp;<a target='_blank' href='readnew.action?id="+art.getArticle_id()+"'>"+pic+"")
							.append("</a>&nbsp;<a target='_blank' href='readnew.action?id="+art.getArticle_id()+"' class='course_color'>"
									+ art.getArea()
									+ "</a></li>");
				}
			}
			//防止对应的课程栏目下面没有信息，进行填充处理
			//首条新闻的填充
			if (i == 0) 
			{
				i++;
				str.append("<li class='mt4'><a href='#' class='red'>【普度】</a><a href='#'>普度教育普度教育-YY-XH-336</a>&nbsp;<a href='#'>"
						+ "</a>&nbsp;<a href='#' class='course_color'>洪山 &nbsp;20课时 &nbsp;6000元</a></li>");
			}
			//余下新闻的填充
			for (int j = i; j < temp; j++) 
			{
				i++;
				if (j != 4) 
				{
					i++;
					str.append("<li><a href='#' class='red'>【普度】</a><a href='#'>普度教育普度教育-YY-XH-336</a>&nbsp;<a href='#'>"
							+ "</a>&nbsp;<a href='#' class='course_color'>洪山 &nbsp;20课时 &nbsp;6000元</a></li>");
				}
				if (j == 4) 
				{
					i++;
					str.append("<li class='mt10'><a href='#' class='red'>【普度】</a><a href='#'>普度教育普度教育-YY-XH-336</a>&nbsp;<a href='#'>"
							+ "</a>&nbsp;<a href='#' class='course_color'>洪山&nbsp;20课时 &nbsp;6000元</a></li>");

				}
			}
			writer.write(str.toString());
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		return result;
	}
	
	public String type(String str)
	{
		Map<String, String>map=new HashMap<String, String>();
			//江岸区
			map.put("0101","江岸一元");
			map.put("0102","江岸大智");
			map.put("0103","江岸车站");
			map.put("0104","江岸四唯");
			map.put("0105","江岸球场");
			map.put("0106","江岸西马");
			map.put("0107","江岸永清");
			map.put("0108","江岸劳动");
			map.put("0109","江岸台北");
			map.put("0110","江岸花桥");
			map.put("0111","江岸二七");
			map.put("0112","江岸新村");
			map.put("0113","江岸丹水池");
			map.put("0114","江岸后湖");
			map.put("0115","江岸塔子湖");
			map.put("0116","江岸谌家矶");
			map.put("0117","江岸经济开发区");
			//江汉区
			map.put("0201","江汉民族");
			map.put("0202","江汉民权");
			map.put("0203","江汉满春");
			map.put("0204","江汉花楼");
			map.put("0205","江汉水塔");
			map.put("0206","江汉前进");
			map.put("0207","江汉民意");
			map.put("0208","江汉新华");
			map.put("0209","江汉北湖");
			map.put("0210","江汉唐家墩");
			map.put("0211","江汉长青");
			map.put("0212","江汉万松");
			map.put("0213","江汉汉兴");
			map.put("0214","江汉江汉经济开发区");
			//硚口区
			map.put("0301","硚口易家墩");
			map.put("0302","硚口韩家墩");
			map.put("0303","硚口宗关");
			map.put("0304","硚口汉水桥");
			map.put("0305","硚口宝丰");
			map.put("0306","硚口古田");
			map.put("0307","硚口汉中");
			map.put("0308","硚口荣华");
			map.put("0309","硚口六角亭");
			map.put("0310","硚口汉正");
			map.put("0311","硚口长丰");
			map.put("0312","硚口开发区");
			//汉阳区
			map.put("0401","汉阳洲头");
			map.put("0402","汉阳晴川");
			map.put("0403","汉阳月湖");
			map.put("0404","汉阳翠微");
			map.put("0405","汉阳建桥");
			map.put("0406","汉阳鹦鹉");
			map.put("0407","汉阳五里墩");
			map.put("0408","汉阳琴断口");
			map.put("0409","汉阳六角亭");
			map.put("0410","汉阳永丰");
			map.put("0411","汉阳江堤");
			map.put("0412","汉阳经济开发区");
			//武昌区
			map.put("0501","武昌民族");
			map.put("0502","武昌民权");
			map.put("0503","武昌满春");
			map.put("0504","武昌花楼");
			map.put("0505","武昌水塔");
			map.put("0506","武昌前进");
			map.put("0507","武昌民意");
			map.put("0508","武昌新华");
			map.put("0509","武昌北湖");
			map.put("0510","武昌唐家墩");
			map.put("0511","武昌长青");
			map.put("0512","武昌万松");
			map.put("0213","武昌汉兴");
			map.put("0214","武昌");
			//青山区
			map.put("0601","青山红钢城");
			map.put("0602","青山新沟桥");
			map.put("0603","青山红卫路");
			map.put("0604","青山冶金");
			map.put("0605","青山钢花村");
			map.put("0606","青山青山");
			map.put("0607","青山武东");
			map.put("0608","青山白玉山");
			map.put("0609","青山厂前");
			map.put("0610","青山工人村");
			map.put("0611","江岸钢都管理委");
			//洪山区
			map.put("0701","洪山珞南");
			map.put("0702","洪山关山");
			map.put("0703","洪山梨园");
			map.put("0704","洪山狮子山");
			map.put("0705","洪山和平");
			map.put("0706","洪山洪山");
			map.put("0707","洪山张家湾");
			map.put("0708","洪山青菱");
			map.put("0709","洪山卓刀泉");
			map.put("0710","洪山天兴乡");
			//东西湖
			map.put("0801","东西湖吴家山");
			map.put("0802","东西湖走马岭");
			map.put("0803","东西湖新沟");
			map.put("0804","东西湖长青");
			map.put("0805","东西湖慈惠");
			map.put("0806","东西湖泾河");
			map.put("0807","东西湖金银湖");
			map.put("0808","东西湖将军路");
			map.put("0809","东西湖东山");
			map.put("0810","东西湖柏泉");
			map.put("0811","东西湖辛安渡");
			map.put("0812","东西湖常青花园新区");
			//汉南区
			map.put("0901","汉南纱帽");
			map.put("0902","汉南湘口");
			map.put("0903","汉南汉南农场");
			map.put("0904","汉南银莲湖农场");
			map.put("0905","汉南东荆");
			map.put("0906","汉南乌金农场");
			map.put("0907","汉南东城垸农场");
			map.put("0908","汉南邓南");
			map.put("0909","汉南开发区");
			//江夏
			map.put("1001","江夏纸坊");
			map.put("1002","江夏金口");
			map.put("1003","江夏郑店");
			map.put("1004","江夏乌龙泉");
			map.put("1005","江夏法泗");
			map.put("1006","江夏安山");
			map.put("1007","江夏湖泗");
			map.put("1008","江夏五里界");
			map.put("1009","江夏山坡乡");
			map.put("1010","江夏舒安乡");
			map.put("1011","江夏梁子湖风景区");
			map.put("1012","江夏金水");
			map.put("1013","江夏庙山");
			map.put("1014","江夏藏龙岛");
			map.put("1015","江夏大桥新区");
			//蔡甸区
			map.put("1101","蔡甸蔡甸");
			map.put("1102","蔡甸张湾");
			map.put("1103","蔡甸侏儒");
			map.put("1104","蔡甸永安");
			map.put("1105","蔡甸奓山");
			map.put("1106","蔡甸大集");
			map.put("1107","蔡甸索河");
			map.put("1108","蔡甸玉贤");
			map.put("1109","蔡甸消泗乡");
			map.put("1110","蔡甸桐湖");
			map.put("1111","蔡甸军山");
			map.put("1112","蔡甸沌口");
			//黄陂区
			map.put("1201","黄陂三里");
			map.put("1202","黄陂横店");
			map.put("1203","黄陂六指");
			map.put("1204","黄陂蔡店乡");
			map.put("1205","黄陂滠口");
			map.put("1206","黄陂大潭");
			map.put("1207","黄陂祁家湾");
			map.put("1208","黄陂蔡家榨");
			map.put("1209","黄陂李家集");
			map.put("1210","黄陂长轩岭");
			map.put("1211","黄陂姚家集");
			map.put("1212","黄陂罗汉寺");
			map.put("1213","黄陂木兰乡");
			map.put("1214","黄陂前川");
			map.put("1215","蔡甸王家河");
			map.put("1216","蔡甸武湖 ");
			map.put("1217","蔡甸天河");
			map.put("1218","蔡甸木兰山风景管理区");
			map.put("1219","蔡甸盘龙城经济发展区");
			//新洲区
			map.put("1301","新洲邾城");
			map.put("1302","新洲旧");
			map.put("1303","新洲潘塘");
			map.put("1304","新洲阳逻");
			map.put("1305","新洲双柳");
			map.put("1306","新洲三店");
			map.put("1307","新洲仓埠");
			map.put("1308","新洲汪集");
			map.put("1309","新洲李集");
			map.put("1310","新洲徐古");
			map.put("1311","新洲凤凰");
			map.put("1312","新洲辛冲");
			return map.get(str);
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public String getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(String recordCount) {
		this.recordCount = recordCount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
